import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-06-01
 * Time: 14:52
 */
public class Test {
    public static void main(String[] args) {
        Set<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);
        set.add(5);
        System.out.println(set);
        System.out.println(set.size());
        System.out.println(set.contains(6));
        System.out.println(set.contains(5));
        Object[] arr = set.toArray();
        System.out.println("-------------");

        for (Object o : arr) {
            System.out.print(o + " ");
        }
        System.out.println();
        System.out.println("---------------");
        Iterator<Integer> iterator = set.iterator();
        //迭代器,用于遍历set
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }

    public static void main1(String[] args) {
        Map<Integer, String> map = new TreeMap<>();
        map.put(1, "ZhangSan");
        map.put(2, "LiSi");
        map.put(3, "WangWu");
        //获取所有的key,返回值是Set<K>
        Set<Integer> set = map.keySet();
        System.out.println("获取所有的key:" + set);
        System.out.println("-------------");
        //获取所有的values
        Collection<String> collections = map.values();
        System.out.println("获取所有的value" + collections);
        System.out.println("-------------");
        //获取所有的key和values
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        System.out.println("获取所有的key和value" + entries);
        System.out.println("-------------");
        //key不能重复,value可以重复

        System.out.println("使用Map.Entry<Integer, String>中的setValue替换前");//
        for (Map.Entry<Integer, String> entry : entries) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
        for (Map.Entry<Integer, String> entry : entries) {
            entry.setValue("111111");
        }
        System.out.println("替换后");
        for (Map.Entry<Integer, String> entry : entries) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
        System.out.println("-------------");
        //类比数组,entries相当于一个集合(数组),集合(数组)中每个类型是Map.Entry<Integer, String>(int)
//        int[] arr = {1, 2, 3, 4};
//        for (int x : arr) {
//            System.out.println(x);
//        }
    }
}
